﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharedLib.WinFormMVCFramework
{
    /// <summary>
    /// Simple WinForm implementation of IView
    /// </summary>
    public class WinFormViewBase : Form
    {
        public event EventHandler<ViewClosingEventArgs> ViewClosing;
        public bool ShowModal()
        {
            return DialogResultToModalResult(ShowDialog());
        }

        public bool ModalResult
        {
            get
            {
                return DialogResultToModalResult(DialogResult);
            }
        }

        public virtual void ApplyLanguage(LanguageManager manager)
        {
            if (manager != null)
            {
                manager.ApplyLanguage(this);
            }
        }

        /// <summary>
        /// Convert System.Windows.Forms.DialogResult to bool
        /// </summary>
        /// <param name="result">The DialogResult to convert</param>
        /// <returns></returns>
        protected virtual bool DialogResultToModalResult(DialogResult result)
        {
            if (result == System.Windows.Forms.DialogResult.OK || result == System.Windows.Forms.DialogResult.Yes) return true;
            return false;
        }

        /// <summary>
        /// Invoke Close event when form closing
        /// </summary>
        /// <param name="e"></param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            ViewClosingEventArgs args = new ViewClosingEventArgs();
            args.Cancel = e.Cancel;
            ViewClosing.InvokeEvent(this, args);
            e.Cancel = args.Cancel;
            if (e.Cancel)
            {
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
        }

        /// <summary>
        /// Refresh the ListBox when an item inside got changed
        /// </summary>
        /// <param name="listBox"></param>
        /// <param name="oldItem"></param>
        /// <param name="newItem"></param>
        protected void UpdateItemInList(ListBox listBox, object oldItem, object newItem)
        {
            if (listBox.Items.Contains(oldItem))
            {
                bool selectChangingItem = false;
                int index = listBox.Items.IndexOf(oldItem);
                if (listBox.SelectedIndex == index) selectChangingItem = true;
                listBox.Items.Remove(oldItem);
                listBox.Items.Insert(index, newItem);
                if (selectChangingItem) listBox.SelectedIndex = index;
            }
        }

        /// <summary>
        /// Refresh the ComboBox when an item inside got changed
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="oldItem"></param>
        /// <param name="newItem"></param>
        protected void UpdateItemInList(ComboBox comboBox, object oldItem, object newItem)
        {
            if (comboBox.Items.Contains(oldItem))
            {
                bool selectChangingItem = false;
                int index = comboBox.Items.IndexOf(oldItem);
                if (comboBox.SelectedIndex == index) selectChangingItem = true;
                comboBox.Items.Remove(oldItem);
                comboBox.Items.Insert(index, newItem);
                if (selectChangingItem) comboBox.SelectedIndex = index;
            }
        }

        /// <summary>
        /// Invoke method on UI thread
        /// </summary>
        /// <param name="method"></param>
        public void InvokeOnUIThread(Action method)
        {
            ControlExtensions.InvokeOnUIThread(this, method);
        }
    }
}
